home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMIBEST4.ADF
/
AmigaBasicStuff
/
BasicSorts
/
BinarySearch
< prev
next >
Wrap
Text File
|
1987-07-22
|
699b
|
30 lines
'
' Binary Search for T$ in x$(l..u%)
' Pre : x$(l..u%) in sorted nondecreasing order
' Post: p%=0 -> t$ is not in x(l..u%)
' p%>0 -> p% < l+1 and x$(p%) = t$
' side effects : u% is altered
'
SUB BinarySearch ( u%,x$(),t$,p% ) STATIC
l = 1
Loop:
IF l > u% THEN p=0 : EXIT SUB
m% = CINT( ( l + u% ) / 2 )
IF x$(m%) < t$ THEN l = m% + 1 : GOTO Loop
IF x$(m%) > t$ THEN u% = m% - 1 : GOTO Loop
REM x$(m)=t$
p%=m%
END SUB
'
' Algorithm from Programming Pearls by Jon Bentley
' from AT&T Bell Laboratories
'
' Implemented in AmigaBasic by Gregory A. Kendall
' from Brendallson Software
'